import requests
import time
import random
import logging
from pathlib import Path
#获取token


class Token():
    def __init__(self):
        # 初始化日志系统
        self._setup_logging()

    def _setup_logging(self):
        """配置日志记录系统"""
        self.logger = logging.getLogger('selenium_automation')
        self.logger.setLevel(logging.INFO)  # 设置日志收集级别
        
        # 确保日志目录存在
        log_dir = Path('project_root')
        log_dir.mkdir(parents=True, exist_ok=True)
        
        # 文件处理器（记录INFO及以上级别）
        file_handler = logging.FileHandler(
            filename=log_dir / 'app.log',
            mode='a',  # 追加模式
            encoding='utf-8'
        )
        
        # 日志格式
        formatter = logging.Formatter(
            "%(asctime)s - %(levelname)s - %(message)s [%(filename)s:%(lineno)d]"
        )
        file_handler.setFormatter(formatter)
        
        # 避免重复添加处理器
        if not self.logger.handlers:
            self.logger.addHandler(file_handler)



    def get_token(self):
        url = 'http://hxzhan.rtcloud.com:32111/sso/api/v1/common/login'
        headers = {
            'content-type':'application/json; charset=utf-8'
        }
        json = {"username":"xiaoqing","password":"N4aGDzp6idb9VbQP6UN77WWUxaw91m1/trWr7qKoy/ewOb6ax5UfKIfvA4qWqWgm"}
        response = requests.post(url=url,headers=headers,json=json)
        return response.json()['data']['token']
    
    def get_time(self):
        return str(int(time.time()))
    
    def get_id(self):
        return str(random.randint(1,10000000000))
    
    def get_header(self):
        header = {
                "rt-request-id":self.get_id(),
                "rt-request-ts":self.get_time(),
                "tm-token":self.get_token()
            }
        return header
    